module.exports = function (grunt) {
    var config = require('./.screeps.json');
    var branch = grunt.option('branch') || config.branch;
    var email = grunt.option('email') || config.email;
    var password = grunt.option('password') || config.password;
    var ptr = grunt.option('ptr') ? true : config.ptr

    grunt.loadNpmTasks('grunt-screeps');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-clean')
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-file-append')

    var currentdate = new Date();

    grunt.log.subhead('Task Start: ' + currentdate.toLocaleString())
    grunt.log.writeln('Branch: ' + branch)

    grunt.initConfig({
        screeps: {
            options: {
                email: email,
                password: password,
                branch: branch,
                ptr: ptr
            },
            dist: {
                src: ['dist/*.js']
            }
        },
        clean: {
            'dist': ['dist', 'test']
        },
        copy: {
            screeps: {
                files: [
                    {
                        expand: true,
                        cwd: 'src/',
                        src: '**',
                        dest: 'dist/',
                        filter: 'isFile',
                        // 修改文件名
                        rename: function (dest, src) {
                            return dest + src.replace(/\//g, '_');
                        }
                    }
                ],
                // 修改require导入名与修改后的文件名一致
                options: {
                    process: function (content, srcpath) {
                        return content.replace(/require\(['"](.*)['"]\)/g, function (s, $1) {
                            curpathlist = srcpath.split('/');
                            $1list = $1.split('/');
                            ptr = curpathlist.length - 1;
                            for (let ss of $1list) {
                                if (ss == '.') continue;
                                if (ss == '..') ptr--;
                                else curpathlist[ptr++] = ss;
                            }
                            return 'require("./' + curpathlist.slice(1, ptr).join('_') + '")';
                        });
                    }
                }
            }
        },
        watch: {
            files: "src/*.*",
            tasks: ['file_append:versioning', 'clean', 'copy:screeps', 'screeps']
        },
        file_append: {
            versioning: {
                files: [
                    {
                        append: "\nglobal.SCRIPT_VERSION = '" + currentdate.toLocaleString() + "'\n",
                        input: 'src/version.js',
                    }
                ]
            }
        }
    });

    grunt.registerTask("watch", ["watch"]);
    grunt.registerTask('default', ['file_append:versioning', 'clean', 'copy:screeps', 'screeps']);
    grunt.registerTask('test', ['file_append:versioning', 'clean', 'copy:screeps']);
}